<template>
	<view class="me-container">
		<view class="userinfo-wrapper">
			<image :src="userInfo.avatarUrl ? userInfo.avatarUrl : defaultAvatar" mode="scaleToFill" class="user-avatar" />
			<view class="user-info">
				<view class="user-name">{{ userInfo.nickname }}</view>
				<view class="user-signature">{{ userInfo.signature }}</view>
			</view>

			<view class="user-setting" @click="toProfileModify">
				<uv-icon name="setting" size="24"></uv-icon>
			</view>
		</view>

		<view class="my-statistics">
			<view class="stat-item">
				<view class="stat-number">12</view>
				<view class="stat-name">创作帖子</view>
			</view>
			<view class="stat-item">
				<view class="stat-number">1.2k</view>
				<view class="stat-name">总阅读量</view>
			</view>
			<view class="stat-item">
				<view class="stat-number">48</view>
				<view class="stat-name">收到的赞</view>
			</view>
		</view>

		<!-- tabs -->
		<view class="tabs">
			<uv-tabs :list="tabList" :lineColor="themeColor" @click="handleChangeTab"></uv-tabs>
		</view>
		<!-- 我的创作 -->
		<view v-if="activeTab === 0" class="my-creation">
			<view v-for="item in postList" :key="item" class="creation-item">
				<image :src="item.images[0].url" mode="scaleToFill" class="creation-cover" />
				<view class="creation-info">
					<view class="creation-title">{{ item.title }}<view class="creation-tag">公开</view> </view>
					<view class="creation-desc">
						<view class="creation-time">
							{{ item.createTime }}
						</view>
						<view class="creation-stat">
							<uv-icon name="eye"></uv-icon>
							<text>{{ item.reads }}</text>
							<uv-icon name="heart"></uv-icon>
							<text>{{ item.likes }}</text>
						</view>
					</view>
				</view>
				<!-- 右上角更多 -->
				<view class="creation-more" @click="handleMore(item)">
					<uv-icon name="more-dot-fill"></uv-icon>
				</view>
			</view>
		</view>

		<!-- 阅读记录 -->
		<view v-show="activeTab === 1" class="read-record">
			<view v-for="item in routeList" :key="item" class="record-item" @click="toRoute(item.id)">
				<image :src="item.route[0].image" class="record-cover" />
				<view class="record-info">
					<view class="record-title">{{ item.title }}</view>
					<view class="route-desc">{{ item.description }}</view>
					<view class="record-details">
						<image src="@/static/icon/site.png" class="icon" />
						<text>{{ item.destination }}</text>
						<image src="@/static/icon/calendar.png" class="icon" />
						<text>{{ item.day }}天</text>
						<image src="@/static/icon/walk.png" class="icon" />
						<text>{{ item.member }}人</text>
					</view>
				</view>
			</view>
		</view>

		<uv-action-sheet ref="actionSheet" :actions="actionSheetList" title="标题" @select="handleSelectAction" round> </uv-action-sheet>

		<!-- Tabbar -->
		<Tabbar :active="4"></Tabbar>
	</view>
</template>

<script>
import request from '@/api/http.js';

export default {
	data() {
		return {
			defaultAvatarUrl:
				'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0',
			userInfo: {
				nickname: '驴哥',
				signature: '旅行达人就是我哈哈哈',
				avatar: '@/static/fengjing1.png'
			},
			tabList: [
				{
					name: '我的帖子'
				},
				{
					name: '我的路线'
				}
			],
			actionSheetList: [
				{
					name: '删除',
					color: '#cf1222'
				},
				{
					name: '设为私密'
				},
				{
					name: '取消' // 开启后文字不显示
				}
			],
			selectedItem: null, // 当前选中的item
			activeTab: 0, // 当前选中的tab
			postList: [], // 我的帖子列表
			routeList: [] // 我的路线列表
		};
	},
	async onShow() {
		await this.handleGetUserData();
		await this.handleGetPostList();
		await this.handleGetRouteList();
	},
	methods: {
		toProfileModify() {
			uni.navigateTo({
				url: '/pages/profileModify/profileModify'
			});
		},
		async handleGetUserData() {
			const res = await request(`/users/getUserData`);
			let userInfo = res.data;
			userInfo.open = userInfo.open === 1 ? true : false;
			userInfo.sex = userInfo.sex === 0 ? '男' : '女';

			this.userInfo = userInfo;
		},
		async handleGetPostList() {
			const res = await request(`/forum/page?thisuser=1`);
			this.postList = res.data.list.map((item) => {
				return {
					...item,
					createTime: item.createTime.slice(0, 10)
				};
			});
		},
		async handleGetRouteList() {
			const res = await request(`/travel/getMyTravelList?userId=1`);
			this.routeList = res.data.data.filter((item) => !item.others);
		},
		handleMore(item) {
			this.selectedItem = item;
			this.$refs.actionSheet.open();
		},
		handleSelectAction(e) {
			if (e.name === '删除') {
				this.$refs.actionSheet.close();
				uni.showModal({
					title: '提示',
					content: '确定删除该绘本吗？',
					success: (res) => {
						if (res.confirm) {
							console.log('用户点击确定');
							// TODO: 删除绘本
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			} else if (e.name === '设为私密') {
				this.$refs.actionSheet.close();
				// TODO: 设为私密
			} else if (e.name === '取消') {
				this.$refs.actionSheet.close();
			}
		},
		handleChangeTab(e) {
			this.activeTab = e.index;
		},
		toRoute(id) {
			uni.navigateTo({ url: '/pages/route/route?id=' + id });
		},
	}
};
</script>

<style>
page {
	background-color: #f8f9fc;
}
</style>

<style lang="scss" scoped>
.me-container {
	.userinfo-wrapper {
		display: flex;
		align-items: center;
		padding: 40rpx;
		background-color: #fff;

		.user-avatar {
			width: 100rpx;
			height: 100rpx;
			border-radius: 50%;
			margin-right: 20rpx;
		}

		.user-info {
			flex: 1;

			.user-name {
				font-size: 32rpx;
				font-weight: bold;
				margin-bottom: 10rpx;
			}

			.user-signature {
				font-size: 24rpx;
				color: #999;
			}
		}

		.user-setting {
			width: 40rpx;
			height: 40rpx;
			line-height: 40rpx;
			text-align: center;
			border-radius: 50%;
			background-color: #f5f5f5;
		}
	}

	.my-statistics {
		display: flex;
		justify-content: space-around;
		padding: 20rpx 0;
		background-color: #fff;

		.stat-item {
			text-align: center;

			.stat-number {
				font-size: 32rpx;
				font-weight: bold;
				margin-bottom: 10rpx;
			}

			.stat-name {
				font-size: 24rpx;
				color: #999;
			}
		}
	}

	.tabs {
		margin-top: 10rpx;
		padding: 0 20rpx;
		background-color: #fff;
	}

	.my-creation {
		width: calc(100% - 40rpx);
		display: grid;
		grid-template-columns: 1fr 1fr; // 两列
		gap: 20rpx; // 间距
		padding: 20rpx;
		background-color: #f8f9fc;

		.creation-item {
			display: flex;
			flex-direction: column;
			position: relative;
			padding: 20rpx;
			border-radius: 25rpx;
			background-color: #fff;
			box-shadow: 0 3rpx 12rpx #eee;

			.creation-cover {
				width: 100%;
				height: 300rpx;
				border-radius: 25rpx;
				overflow: hidden;
				object-fit: cover;
			}

			.creation-info {
				display: flex;
				flex-direction: column;
				margin-top: 20rpx;

				.creation-title {
					width: 300rpx;
					display: flex;
					align-items: center;
					font-size: 28rpx;
					font-weight: bold;
					margin-bottom: 10rpx;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;

					.creation-tag {
						display: inline-block;
						padding: 2rpx 10rpx;
						background-color: $theme-color;
						color: #fff;
						border-radius: 8rpx;
						margin-left: 10rpx;
						font-size: 20rpx;
						font-weight: normal;
					}
				}

				.creation-desc {
					display: flex;
					justify-content: space-between;
					align-items: center;

					.creation-time {
						font-size: 20rpx;
						color: #999;
					}

					.creation-stat {
						display: flex;
						align-items: center;

						.uv-icon {
							margin-right: 10rpx;
						}

						text {
							font-size: 20rpx;
							color: #999;
							margin-right: 10rpx;
						}
					}
				}
			}

			.creation-more {
				position: absolute;
				top: 30rpx;
				right: 30rpx;
				width: 50rpx;
				height: 50rpx;
				line-height: 40rpx;
				text-align: center;
				border-radius: 50%;
				background-color: #eeeeeeee;
				color: #fff;
				display: flex;
				justify-content: center;
				align-items: center;
			}
		}
	}

	.read-record {
		padding: 20rpx;
		background-color: #fff;

		.record-item {
			display: flex;
			align-items: center;
			padding: 20rpx;
			border-bottom: 1rpx solid #eee;

			.record-cover {
				width: 100rpx;
				min-width: 100rpx;
				height: 100rpx;
				border-radius: 10rpx; // 圆角
				overflow: hidden;
				margin-right: 20rpx;
			}

			.record-info {
				display: flex;
				flex-direction: column;

				.record-title {
					font-size: 28rpx;
					font-weight: bold;
					width: 500rpx;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
				}

				.route-desc {
					font-size: 24rpx;
					color: #999;
					font-weight: normal;
					display: -webkit-box;
					-webkit-box-orient: vertical;
					-webkit-line-clamp: 2;
					overflow: hidden;
					margin-bottom: 10rpx;
				}

				.record-details {
					display: flex;
					align-items: center;
					font-size: 24rpx;
					color: #999;

					image {
						width: 28rpx;
						height: 28rpx;
						margin-right: 5rpx;
					}
					text {
						color: #999;
						margin-right: 40rpx;
					}
				}
			}

			.record-time {
				font-size: 24rpx;
				color: #999;
			}
		}
	}
}
</style>
